package com.example.demo.serviceimp;

import com.example.demo.entity.QuestionsOptions;
import com.example.demo.entity.TestQuestions;
import com.example.demo.mapper.QuestionsMapper;
import com.example.demo.service.QuestionsService;
import com.example.demo.utils.R;
import com.example.demo.utils.ReSet;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class QuestionsServiceImp implements QuestionsService {

    @Resource
    private QuestionsMapper questionsMapper;




    //查询章节试题
    @Override
    public List<TestQuestions> selsectlist(Integer chapter_id) {
        return questionsMapper.selsectlist(chapter_id);
    }
    @Override
    public TestQuestions selsect(Integer test_questions_id) {
        return questionsMapper.selsect(test_questions_id);
    }
    @Override
    public List<QuestionsOptions> selopts(Integer test_questions_id) {
        return questionsMapper.selopts(test_questions_id);
    }

    /**
     * 查询试卷题目
     * */
    @Override
    public String seltpque(Integer cid) {

        System.out.println(questionsMapper.seltpque(cid));

        if (questionsMapper.seltpque(cid) != null && questionsMapper.seltpque(cid) != ""){
            return questionsMapper.seltpque(cid);
        }else{
            return "数据为空";
        }


    }//找到试卷名称
//
//    @Override
//    public int seltpqueid(Integer cid) {
//
//        if (questionsMapper.seltpqueid(cid) > 0){
//            return questionsMapper.seltpqueid(cid);
//        }else{
//            return 0;
//        }
//
//    }//找到试卷ID

    public List selpptid(Integer cid){return questionsMapper.selpptid(cid);}//找到试卷对应的题目ID
    public String seltqname(Integer tqid){return questionsMapper.seltqname(tqid);}//找到试卷名称
    public List selqops(Integer tqid){return questionsMapper.selqops(tqid);}//找到题目选项

    //查询所有试题
    @Override
    public List<TestQuestions> selquestion(Integer page,Integer limit) {
        page = (page - 1) * limit;
        return questionsMapper.selquestion(page,limit);
    }

    @Override
    public List<TestQuestions> selquestion1() {
        return questionsMapper.selquestion1();
    }

    @Override
    public int selquenums() {
        return questionsMapper.selquenums();
    }

    @Override
    public List<TestQuestions> selmuhu(String test_questions_name,Integer test_questions_picture,Integer test_questions_history) {
//        if (questionsMapper.selmuhu(qname, tqp, tqh).size() > 0){
            return questionsMapper.selmuhu(test_questions_name, test_questions_picture, test_questions_history);
//        }
//        List list = new ArrayList();
//        list.add("查询失败");
//        return list;
    }


    //查询试题答案
    @Override
    public List<QuestionsOptions> selqueopt(Integer test_questions_id){
        return questionsMapper.selqueopt(test_questions_id);
    }

    //增加试题
    @Override
    public int insertquestion(String test_questions_name, Integer test_questions_difficulty, Integer test_questions_language, Integer test_questions_picture, Integer test_questions_history, Integer test_questions_score) {
        return questionsMapper.insertquestion(test_questions_name,test_questions_difficulty,test_questions_language,test_questions_picture,test_questions_history,test_questions_score);
    }

    //增加试题答案
    @Override
    public int inserqueopt(Integer test_questions_id, String questions_options_content, Integer questions_options_type) {
        return questionsMapper.inserqueopt(test_questions_id,questions_options_content,questions_options_type);
    }

    //增加章节试题
    @Override
    public int insertpaperques(Integer test_paper_id, Integer test_questions_id) {
        return questionsMapper.insertpaperques(test_paper_id,test_questions_id);
    }

    //增加试卷试题
    @Override
    public int inspques(Integer cid, Integer tqid) {
        return questionsMapper.inspques(cid, tqid);
    }

    @Override
    public int selspques(Integer cid, Integer tqid) {
        return questionsMapper.selspques(cid, tqid);
    }

    //更改试题
    @Override
    public int updatetestques(String test_questions_name, Integer test_questions_difficulty, Integer test_questions_picture, Integer test_questions_history, Integer test_questions_score, Integer test_questions_id) {
        return questionsMapper.updatetestques(test_questions_name,test_questions_difficulty,test_questions_picture,test_questions_history,test_questions_score,test_questions_id);
    }

    //删除试题
    @Override
    public int deleteques(Integer test_questions_id) {
        return questionsMapper.deleteques(test_questions_id);
    }

    //删除章节中的试题
    @Override
    public int delcques(Integer test_questions_id) {
        return questionsMapper.delcques(test_questions_id);
    }

    @Override
    public int selcques(Integer test_questions_id) {
        return questionsMapper.selcques(test_questions_id);
    }

    @Override
    public int delpicques(Integer test_questions_id) {
        return questionsMapper.delpicques(test_questions_id);
    }

    @Override
    public int selpicques(Integer test_questions_id) {
        return questionsMapper.selpicques(test_questions_id);
    }

    //删除试题答案
    @Override
    public int delqueopt(Integer test_questions_id) {
        return questionsMapper.delqueopt(test_questions_id);
    }

    //查询试题ID
    @Override
    public int selque(String name) {
        return questionsMapper.selque(name);
    }

    //删除章节试题
    @Override
    public int delsecques(Integer chapter_id, Integer paper_question_id) {
        return questionsMapper.delsecques(chapter_id,paper_question_id);
    }

    //上传试题图片
    @Override
    public int intpic(String question_picture_name,String question_picture_address, Integer test_questions_id) {
        return questionsMapper.intpic(question_picture_name,question_picture_address,test_questions_id);
    }

    //文件上传
    @Override
    public R upload(MultipartFile file, Integer test_questions_id) {
        if(file.isEmpty()){
            return ReSet.Err();
        }
        //文件的原名
        String OriginalFilename = file.getOriginalFilename();
        //根据时间戳对文件进行重新命名
        String fileName = System.currentTimeMillis()+"."+OriginalFilename.substring(OriginalFilename.lastIndexOf(".")+1);
//        String filePath="D:/files/demo/target/classes/static\\";
        String filePath="D:/files/SysExam/target/classes/static\\";
        File dest = new File(filePath+fileName);
        //得到文件的
        String dz = "http://localhost:8080/";
        String question_picture_address = dz+dest.getName();

        if(dest.getParentFile().exists()){
            dest.getParentFile().mkdirs();
        }
        //调用接口把对应的值与路径存起
        intpic(OriginalFilename,question_picture_address,test_questions_id);
        //把从远处文件复制到上传位置来
        try{
            file.transferTo(dest);
        } catch (IOException e) {
            e.printStackTrace();
            return ReSet.Err();
        }
        return ReSet.Ok(fileName);
    }

    /**
     * 查询是否正确答案
     * */
    @Override
    public int seloptty(Integer questions_options_id) {
        return questionsMapper.seloptty(questions_options_id);
    }


    /**
     * 更改试题选项
     */
    @Override
    public int upoptionall(Integer qoid) {
        if (questionsMapper.upoptionall(qoid) > 0){
            return questionsMapper.upoptionall(qoid);
        }
        return 0;
    }

    @Override
    public int upoption(String qocent,Integer qotype, Integer qoid,Integer tqid) {
        if (questionsMapper.upoption(qocent,qotype, qoid,tqid) > 0){
            return questionsMapper.upoption(qocent,qotype, qoid,tqid);
        }
        return 0;
    }

    @Override
    public int selcqion(Integer cid, Integer qid) {
        return questionsMapper.selcqion(cid, qid);
    }

    @Override
    public int inscqion(Integer cid, Integer qid) {
        return questionsMapper.inscqion(cid, qid);
    }

    @Override
    public List<Integer> selcqus(Integer cid) {
        return questionsMapper.selcqus(cid);
    }

    @Override
    public List<TestQuestions> seltqnm(Integer tqid) {
        return questionsMapper.seltqnm(tqid);
    }

    /**
     * 查询试题图片
     * @param tqid
     * @return
     */
    @Override
    public List selqucpic(Integer tqid) {
        return questionsMapper.selqucpic(tqid);
    }


}
